/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 * 
 */
package com.niannian.activity;

import com.niannian.view.CropImage;
import com.niannian.view.CropImageView;
import com.niannian.util.BitmapUtil;
import com.niannian.util.Common;
import com.niannian.BaseActivity;
import com.niannian.MyApplication;
import com.niannian.R;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ImageFilterCropActivity extends BaseActivity {

	private CropImageView mDisplay;
	private ProgressBar mProgressBar;
	private LinearLayout mLeft;
	private LinearLayout mRight;

	public static final int SHOW_PROGRESS = 0;
	public static final int REMOVE_PROGRESS = 1;

	private String mPath;
	private Bitmap mBitmap;
	private CropImage mCropImage;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imagefilter_cut);
		addActToGroup("ALL", this);
		findViewById();
		setListener();
		init();
		initTopView();
	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left.setImageResource(R.drawable.cancel);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("修改头像");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.VISIBLE);
		iv_top_right.setImageResource(R.drawable.yes);
		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myfinish();
			}
		});
		iv_top_right.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String imgPath = BitmapUtil.saveBmp(mCropImage.cropAndSave(), MyApplication.FILE_PATH_TEMP, USER_HEAD_IMAGE_DEFAULT, 100);
				Intent intent = new Intent();
				if (imgPath != null) {
					intent.putExtra("path", imgPath);
				}
				setResult(RESULT_OK, intent);
				myfinish();
			}
		});
	}

	private void findViewById() {

		mDisplay = (CropImageView) findViewById(R.id.imagefilter_crop_display);
		mProgressBar = (ProgressBar) findViewById(R.id.imagefilter_crop_progressbar);
		mLeft = (LinearLayout) findViewById(R.id.imagefilter_crop_left);
		mRight = (LinearLayout) findViewById(R.id.imagefilter_crop_right);
	}

	private void setListener() {

		mLeft.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mCropImage.startRotate(270.f);
			}
		});
		mRight.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mCropImage.startRotate(90.f);
			}
		});
	}

	private void init() {
		mPath = getIntent().getStringExtra("path");
		try {
			mBitmap = BitmapUtil.createBitmap(mPath, mScreenHeight, mScreenWidth);
			if (mBitmap == null) {
				Common.tip(ImageFilterCropActivity.this, "图片为空");
				setResult(RESULT_CANCELED);
				myfinish();
			} else {
				resetImageView(mBitmap);
			}
		} catch (Exception e) {
			Common.tip(ImageFilterCropActivity.this, "图片有误");
			setResult(RESULT_CANCELED);
			myfinish();
		}
	}

	private void resetImageView(Bitmap bitmap) {
		mDisplay.clear();
		mDisplay.setImageBitmap(bitmap);
		mDisplay.setImageBitmapResetBase(bitmap, true);
		mCropImage = new CropImage(this, mDisplay, handler);
		mCropImage.crop(bitmap);
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOW_PROGRESS:
				mProgressBar.setVisibility(View.VISIBLE);
				break;
			case REMOVE_PROGRESS:
				handler.removeMessages(SHOW_PROGRESS);
				mProgressBar.setVisibility(View.INVISIBLE);
				break;
			}
		}
	};

}
